/**
 * 
 */
package com.et114.core.control;
//import org.springframework.util.Assert;

import java.net.URL;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.et114.core.common.CONST;

//import org.springframework.beans.factory.FactoryBean;

/**
 * @author guanhw
 */
public class ActionFactoryBean implements IAppInitBean  {
	private org.springframework.core.io.Resource tokenFile ;
	private String saveToken = "saveToken" ;
	private String checkToken = "checkToken" ;
	private Log logger = LogFactory.getLog( this.getClass() );
	public static ITokenMap tokenMap ; 
	/**
	 * 
	 */
	public ActionFactoryBean ( ) {
		// TODO Auto-generated constructor stub
	}
	
	/* (non-Javadoc)
	 * @see com.et114.core.control.IAppInitBean#init()
	 */
	public void init ( )  throws Exception {
		// TODO Auto-generated method stub
		logger.info ( "init token config !" );
		if ( tokenFile != null ) {
			URL url = tokenFile.getURL ( );
			File file = new File (  url.getFile ( ) );
			if ( file !=null && file .isDirectory ( ) ) {
				for ( int i = 0 ; i < 2 ; i ++ ) {
					File _file  = new File( file.getAbsolutePath ( ) + File.separator + ( i == 0 ? saveToken : checkToken ) + ".properties" ) ;
					if ( _file.exists ( ) && _file.canRead ( ) ) {
						InputStream input = new FileInputStream( _file ) ; 
						Properties p = new Properties();
						p.load ( input ) ;
						input.close ( );
						tokenMap = new TokenMapImpl();
						Set keys = p.keySet ( );
						Iterator ites = keys.iterator ( );
						while ( ites.hasNext ( ) ) {
							String uri = ites.next ( ).toString ( );
							if ( i ==0  ) {
								tokenMap.appendMap ( uri ,  CONST.SAVE_TOKEN );
							}
								
							if ( i == 1 ) {
								tokenMap.appendMap ( uri ,  CONST.CHECK_TOKEN );
							}
						}
						p.clear ( );
					}
					else{
						throw new Exception( "令牌文件" + _file + " 不能读或不存在！" ) ; 
					}
				}
			}
		}
		logger.info ( "init token config end !" );
	}

	public org.springframework.core.io.Resource getTokenFile ( ) {
		return tokenFile;
	}

	public void setTokenFile ( org.springframework.core.io.Resource tokenFile ) {
		this.tokenFile = tokenFile;
	}

	public String getSaveToken ( ) {
		return saveToken;
	}

	public void setSaveToken ( String saveToken ) {
		this.saveToken = saveToken;
	}

	public ITokenMap getTokenMap ( ) {
		return tokenMap;
	}


	public String getCheckToken ( ) {
		return checkToken;
	}

	public void setCheckToken ( String checkToken ) {
		this.checkToken = checkToken;
	}


}
